package com.rongshu.view.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.utils.DateUtils;
import com.rongshu.utils.GlideUtils;
import com.rongshu.view.home.activity.DaySignDetailsActivity;
import com.rongshu.view.home.entity.DaySignListResponse;

import java.util.Date;
import java.util.List;

/**
 * created by zhengweis on 2019/2/20.
 * description：
 */
public class DaySignListAdapter extends RecyclerView.Adapter<DaySignListAdapter.ViewHolder> {
    private Context context;
    private List<DaySignListResponse.ResponseBean> list;
    private int type;

    public DaySignListAdapter(Context context, List<DaySignListResponse.ResponseBean> list, int type) {
        this.list = list;
        this.context = context;
        this.type = type;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_day_sign_list, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
        GlideUtils.loadImageView(context, viewHolder.signImg, list.get(i).getImgUrl()
                , R.drawable.penpal_default_logo, R.drawable.penpal_default_logo);
        Date date = DateUtils.parseDate(DateUtils.YYYY_MM_DD_HH_MM_SS, list.get(i).getDailyDate());
        String formatDate = DateUtils.formatDate(DateUtils.YYYY_MM_DD, date);
        viewHolder.signDate.setText(formatDate);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(context, DaySignDetailsActivity.class);
                intent.putExtra(Constant.INTENT_TYPE, type);
                intent.putExtra(Constant.INTENT_DATA, list.get(i).getId());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView signDate;
        private ImageView signImg;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            signImg = itemView.findViewById(R.id.sign_img);
            signDate = itemView.findViewById(R.id.sign_date);
        }
    }
}
